﻿using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using XFControls.XFPanels;

namespace XFSenseUI.XFItems.SenseControls
{
    public class SenseControlNumberBig : SenseItemBase
    {
        private int _height;
        private bool _down;
        private bool _increase;
        private bool _close;
        private int _numbWidth;

        public int Value { get; set; }
        public int MaxValue = int.MaxValue;
        public int MinValue = int.MinValue;

        public SenseControlNumberBig()
        {
            Style = new XFItemStyle
                    {
                TextFont = new Font(FontFamily.GenericSansSerif, 22, FontStyle.Regular),
                TextColor = Color.Black,
                Padding = 15,
                PaddingTop = 18
            };

            ItemRectangle = new Rectangle();
            _height = -1;
            ItemType = XFItemType.Clickable;
        }

        public override void OnAdd()
        {
            base.OnAdd();

            var rec = this.GetImageRec("counter-two-modal");
            rec.X = (GetWidth() >> 1) - (rec.Width >> 1);
            rec.Y = (Parent.Parent.Height >> 1) - (rec.Height >> 1);
            ItemRectangle = rec;
            _numbWidth = Style.TextFont.MeasureString("0", new Rectangle(0, 0, 100, 0)).Width;
        }

        public override int GetHeight()
        {
            return Parent.Parent.Height;
        }

        public override void ResetHeight() { }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            int number = 100;
            int iterations = 0;

            while (number < Value)
            {
                iterations++;
                number *= 10;
            }

            int centWidth = (iterations * _numbWidth);

            var leftRec = new Rectangle(ItemRectangle.X - centWidth, ItemRectangle.Y, ItemRectangle.Width >> 1, ItemRectangle.Height);
            var centRec = new Rectangle(leftRec.X + leftRec.Width, ItemRectangle.Y, centWidth, ItemRectangle.Height);
            var rightRec = new Rectangle(centRec.X + centWidth, ItemRectangle.Y, ItemRectangle.Width >> 1, ItemRectangle.Height);

            DrawUI("counter-modal-left", g, leftRec);
            DrawUI("counter-modal-center", g, centRec, false);
            DrawUI("counter-modal-right", g, rightRec);

            using (SolidBrush b = new SolidBrush(Style.TextColor))
                g.DrawString(Value.ToString(), Style.TextFont, b, ItemRectangle.X + Style.PaddingLeft, ItemRectangle.Y + Style.PaddingTop);
        }

        public override XFItemClickResult GetClickResult()
        {
            if (_close)
                Parent.ClearEffect(this);

            return XFItemClickResult.Nothing;
        }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);

            if (!ItemRectangle.Contains(new Point(x, y)))
            {
                _close = true;
                return;
            }
            _close = false;

            _down = true;
            int half = GetHeight() >> 1;

            _increase = y < half;

            while (Parent != null && Parent.State != States.Idle)
            {
                if ((_increase && Value + 1 > MaxValue) || (!_increase && Value - 1 < MinValue))
                    return;
                
                Value += _increase ? 1 : -1;

                if (Value >= 10)
                    Style.PaddingLeft = 10;
                else if (Value >= 100)
                    Style.PaddingLeft = 0;
                else
                    Style.PaddingLeft = 15;

                Invalidate();
                    Parent.Invalidate();

                Thread.Sleep(200);
                Application.DoEvents();
            }
        }

        public override void OnBlur()
        {
            base.OnBlur();
            _down = false;
        }

        public override void OnMouseMove(int x, int y)
        {
            base.OnMouseMove(x, y);
            int half = GetHeight() >> 1;

            _increase = y < half;
        }

        public override void OnMouseUp()
        {
            base.OnMouseUp();
            _down = false;
            if (_close)
                Parent.ClearEffect(this);
        }
    }
}
